<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Admin extends CI_Controller {
    
	function Admin()
	{
		parent::__construct();

		// $this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="help-inline">', '</div>');
		
		if(!$this->session->userdata('logged_in'))
		{
			//If no session, redirect to login page
			redirect(base_url('login'), 'refresh');
		}
		$session_data = $this->session->userdata('logged_in');
		$this->smarty->assign('username', $session_data['username']);
		$this->smarty->assign('base_url', base_url());
	}

	public function index()
	{
		
            $this->load->model('navigation');
            $articles = $this->navigation->get_last_ten_entries();
           // $this->load->view('admin',$articles);
            $this->smarty->assign('articles',json_decode(json_encode($articles),true));
          
            $this->smarty->view('admin.tpl');
	}
	
	public function new_entry()
	{
            $data = array();
            $this->load->model('navigation');
           // $this->load->view('admin',$articles);
            $this->smarty->assign('section','new_entry');
            $this->smarty->assign('insertURL',base_url('admin/new_entry'));
           
            $this->smarty->assign('levels', $this->navigation->get_levels());
            
            // Set the validation rules if this is a submit
            if ( $this->input->post('action') == 'submit' )
            {
                    $this->form_validation->set_rules('navigation_name', 'Identificator', 'trim|required|min_length[2]|max_length[32]|xss_clean');
                    $this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[2]|max_length[100]|xss_clean');
                   /* $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
                    $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
                    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
                    $this->form_validation->set_rules('state', 'State', '');*/

                    if ( ! $this->form_validation->run() )
                    {
                            $data['error'] = 'Check and fix the form errors below';
                    }
                    else
                    {
                        $this->insert_entry();
                        $data['message'] = 'Thanks for posting!';
                    }
            }
            $data['base_url'] = base_url();
            $this->smarty->view('admin.tpl',$data);
	}
        
	private function insert_entry()
	{            
            $this->navigation_name = $this->input->post('navigation_name');
            $this->parent_id = $this->input->post('parent_id');
            $this->title = $this->input->post('title');
            $this->description = $this->input->post('description');
            $this->url = '';
            $this->language = $this->input->post('language');
            $this->date = date("Y-m-d H:i:s");
            $this->active = '1';
            $this->type = 'static';
            $this->static_content = $this->input->post('static_content');
            
            $this->load->model('navigation');     
            $this->navigation->insert_entry($this);
	}   
        
}
